package com.example.springboottest.example.jvm;

/**
 * 启动参数加上 -XX:+PrintGC -Xms32m -Xmx32m
 */
public class PrintGC {
    public static void main(String[] args) {
        PrintGC PrintGC = new PrintGC();
        PrintGC.localVarGC2();
    }

    public void localVarGC1() {
        byte[] arr = new byte[6 * 1024 * 1024];
        System.gc();
    }

    /**
     * [0.007s][info   ][gc] Using G1
     * 第一次垃圾回收，堆从占用19M变成了占用9M，释放了大约10M
     * [0.202s][info   ][gc] GC(0) Pause Full (System.gc()) 19M->9M(32M) 3.758ms
     * [0.203s][info   ][gc] GC(1) Pause Young (Concurrent Start) (G1 Humongous Allocation) 9M->9M(32M) 0.259ms
     * [0.203s][info   ][gc] GC(2) Concurrent Cycle
     * 第二次垃圾回收，堆变成了占用16M,因为又申请了6M
     * [0.205s][info   ][gc] GC(2) Pause Remark 16M->16M(32M) 0.668ms
     * [0.210s][info   ][gc] GC(3) Pause Full (System.gc()) 16M->16M(32M) 4.634ms
     * [0.211s][info   ][gc] GC(2) Concurrent Cycle 7.795ms
     */
    public void localVarGC2() {
        byte[] arr = new byte[6 * 1024 * 1024];
        arr = null;
        System.gc();
        byte[] arr2 = new byte[6 * 1024 * 1024];
        System.gc();
    }
}
